package cn.whdream.juc;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

public class TestForkJoinPool {
	public static void main(String[] args) {
		Instant start = Instant.now();
		ForkJoinPool pool = new ForkJoinPool();
		ForkJoinTask<Long> calculate = new ForkJoinSumCalculate(0L, 100000000L);
		Long sum = pool.invoke(calculate);
		Instant end = Instant.now();
		System.out.println(sum+"耗费时间："+Duration.between(start, end).toMillis());
	}
	private static class ForkJoinSumCalculate extends RecursiveTask<Long>{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private long start;
		private long end;
		private static final long THURSHOLD = 10000L;
		
		public ForkJoinSumCalculate(Long start, Long end) {
			super();
			this.start = start;
			this.end = end;
		}

		@Override
		protected Long compute() {
			long length = end -start;
			if(length <= THURSHOLD) {
				long sum = 0L;
				for (long i = start; i <= end; i++) {
					sum += i;
				}
				return sum;
			}else {
				long middle = (start + end)/2;
				ForkJoinSumCalculate left = new ForkJoinSumCalculate(start, middle);
				left.fork();
				ForkJoinSumCalculate right = new ForkJoinSumCalculate(middle+1, end);
				right.fork();
				return left.join()+right.join();
			}
		}
		
	}
}
